package com.silence.qhyh.webpro.mvp.ui.activity.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.silence.qhyh.webpro.R;
import com.silence.qhyh.webpro.app.utils.GetJsonDataUtil;
import com.silence.qhyh.webpro.mvp.model.entity.JsonBean;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.view.RxTitle;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;

/**
 * @author silence
 * 编辑收货地址
 */
public class EditAddressActivity extends BaseActivity {

    @BindView(R.id.rx_title)
    RxTitle rxTitle;
    @BindView(R.id.et_consignee)
    EditText etConsignee;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.app_detailed_address_hint)
    EditText appDetailedAddressHint;
    @BindView(R.id.ll_del_address)
    RelativeLayout llDelAddress;
    @BindView(R.id.sb_save)
    SuperTextView sbSave;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_edit_address;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rxTitle.setLeftFinish(this);
        initJsonData();
    }

    @OnClick({R.id.tv_area, R.id.ll_del_address, R.id.sb_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_area:
                showPickerView();
                break;
            case R.id.ll_del_address:
                break;
            case R.id.sb_save:
                RxActivityTool.skipActivity(this, OrderSuccessActivity.class);
                break;
            default:
                break;
        }
    }

    private List<JsonBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();

    /**
     * 解析城市
     */
    private void initJsonData() {
        Observable.empty().observeOn(AndroidSchedulers.mainThread()).doOnComplete(new Action() {
            @Override
            public void run() throws Exception {
                String JsonData = new GetJsonDataUtil().getJson(EditAddressActivity.this, "province.json");//获取assets目录下的json文件数据
                ArrayList<JsonBean> jsonBean = parseData(JsonData);//用Gson 转成实体
                options1Items = jsonBean;
                for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
                    ArrayList<String> cityList = new ArrayList<>();//该省的城市列表（第二级）
                    ArrayList<ArrayList<String>> province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）
                    for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍历该省份的所有城市
                        String cityName = jsonBean.get(i).getCityList().get(c).getName();
                        cityList.add(cityName);//添加城市
                        ArrayList<String> city_AreaList = new ArrayList<>();//该城市的所有地区列表
                        city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
                        province_AreaList.add(city_AreaList);//添加该省所有地区数据
                    }
                    options2Items.add(cityList);
                    options3Items.add(province_AreaList);
                }
            }
        }).subscribe();
    }

    public ArrayList<JsonBean> parseData(String result) {
        ArrayList<JsonBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }

    /**
     * 选择城市
     */
    private void showPickerView() {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String opt1tx = options1Items.size() > 0 ?
                        options1Items.get(options1).getPickerViewText() : "";

                String opt2tx = options2Items.size() > 0
                        && options2Items.get(options1).size() > 0 ?
                        options2Items.get(options1).get(options2) : "";

                String opt3tx = options2Items.size() > 0
                        && options3Items.get(options1).size() > 0
                        && options3Items.get(options1).get(options2).size() > 0 ?
                        options3Items.get(options1).get(options2).get(options3) : "";
                String tx = opt1tx + opt2tx + opt3tx;
                tvArea.setText(tx);
            }
        })
                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();
        pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        pvOptions.show();
    }
}
